import request from '../../utils/request'
let startY = 0; // 手指起始坐标
let moveY = 0; //手指移动坐标
let moveDistance = 0 //手指移动的距离
Page({

  /**
   * 页面的初始数据
   */
  data: {
    coverTranform: 'translateY(0)',
    coverTransition: '',
    userInfo: {}, //用户的基本信息
    recentPlayList:[], //用户播放记录
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
   
  },

  handleTouchStart(e) {
    this.setData({
      coverTransition: ''
    })
    // 获取手指起始坐标
    startY = e.touches[0].clientY;
    console.log('s', startY)
  },
  handleTouchMove(e) {
    // 获取手指移动坐标
    moveY = e.touches[0].clientY;
    moveDistance = moveY - startY
    if (moveDistance <= 0) return
    if (moveDistance >= 80) moveDistance = 80
    //动态更新 coverTranform
    this.setData({
      coverTranform: `translateY(${moveDistance}rpx)`
    })
    console.log('m', moveDistance)

  },
  handleTouchEnd() {
    this.setData({
      coverTranform: `translateY(0rpx)`,
      coverTransition: 'transform .5s linear'
    })
    console.log('e')
  },

  // 跳转 login
  toLogin() {
    wx.navigateTo({
      url: '/pages/login/login',
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
     // 读取存储在本地用户的基本信息
     let userInfo = wx.getStorageSync('userInfo')
     if (userInfo) {
       this.setData({
         userInfo: JSON.parse(userInfo)
       })
       //获取用户播放记录
       this.getUserRecentPlayList(this.data.userInfo.userId)
     }
  },
  async getUserRecentPlayList(userId){
    let recentPlayListData = await request('/user/record',{uid:userId,type:1})
    let index = 0;
    let recentPlayList = recentPlayListData.weekData.splice(0,20).map(item =>{
      item.id = index++
      return item;
    })
    this.setData({
      recentPlayList:recentPlayList
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})